
<template>
    <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">            
            <el-form-item label="名称" prop="name" >
                <el-input v-model="form.name" placeholder="请输入名称"  />
            </el-form-item>            
        </el-form>        
    </div>
</template>
<script>
import {
    getDemoEntityItem,
    createDemoEntityItem,
    updateDemoEntityItem,    
} from "@/api/demo/demoEntityItem";
import { getUserProfile } from "@/api/system/user";
import {genCode} from '@/api/code.js';
import dayjs from 'dayjs'
export default {
    name: "DemoEntityItemEditor",
    components:{        
    },
    props: [        
        "entityId",        
    ],
    data() {
        return {            
            // 表单参数
            form: {},
            params: {},
            // 表单校验
            rules: {                
            },
            user:{}
        };
    },
    methods: {
        // 表单重置
        async reset() {            
            // 重新加载选项            
            // 重置表单值
            this.form = {                
                id: undefined,                
                name: undefined,                
                entityId: undefined,                
            };
            // 重置相关参数            
            this.resetForm("form");
        },        
        async show(params){
            await this.reset();
            getDemoEntityItem(params.id).then(response => {
                let data = response.data;                
                this.form = data;                
            });
        },        
        // 提前提交
        precreate(){
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (!this.form.id) {
                        createDemoEntityItem(this.form).then(response => {
                            if (response.code === 0) {
                                this.form.id = response.data.id;
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        // 正常提交
        submitForm: function(done) {
            this.$refs["form"].validate(valid => {
                if (valid) {                    
                    this.form.entityId = this.entityId;                    
                    if (this.form.id != undefined) {
                        updateDemoEntityItem(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("修改成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        createDemoEntityItem(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("新增成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        genCode(){            
        },
        getUser() {
            return getUserProfile().then(response => {
                this.user = response.data;
            });
        },        
    }
};
</script>
<style>    
</style>